package com.bl.pet.service.impl;

import com.bl.org.domain.Department;
import com.bl.pet.domain.Pet;
import com.bl.pet.domain.PetType;
import com.bl.pet.mapper.PetTypeMapper;
import com.bl.pet.service.IPetTypeService;
import com.bl.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {
    @Autowired
    private PetTypeMapper petTypeMapper;

    @Override
    public List<PetType> typeTree() {
        List<PetType> list =  petTypeMapper.loadAll();

        Map<Long,PetType> map = new HashMap<>();
        for (PetType petType : list) {
            map.put(petType.getId(),petType);
        }

        List<PetType> typeTree = new ArrayList<>();

        for (PetType petType : list) {
            if (petType.getPid()==null){
                // 最高级类型
                typeTree.add(petType);
            }else {
                // 低级类型
                Long pid = petType.getPid();
                // 找到高级类型id
                PetType petType1 = map.get(pid);
                petType1.getChildren().add(petType);
            }
        }
        return typeTree;
    }
}
